﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using ET;
using HTML;

namespace ERP.PageUC
{
    public partial class branchUC : UserControl
    {
        #region[Define]
        protected string StrAlert = string.Empty;
        private readonly Branch_BLL clsBranch = new Branch_BLL();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetBranch();
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                var chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                if (chkHide.Checked)
                {
                    try
                    {
                        var dataKey = ListViewAll.DataKeys[i];
                        if (dataKey != null) Branch_BLL.DeleteBranch(int.Parse(dataKey.Value.ToString()));
                    }
                    catch
                    {
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Chứng từ này còn tồn tại !";
                        OpenDialog("#thongbao");
                        ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            GetBranch();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert = Alert.AlertWarning("Vui lòng nhập vào từ khoá tìm kiếm !");
            }
            else
            {
                GetBranch();
            }
        }
        protected void btnReload_Click(object sender, EventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetBranch();
        }
        #region[Function]
        public void GetBranch()
        {
            DataTable dtBranch = clsBranch.Search(txtSearch.Text);
            if (dtBranch != null)
            {
                ListViewAll.DataSource = dtBranch;
                ListViewAll.DataBind();
                if (txtSearch.Text != "" && dtBranch.Rows.Count > 0)
                {
                    StrAlert = Alert.AlertSucess(dtBranch.Rows.Count.ToString());
                }
            }
            else
            {
                StrAlert = Alert.AlertError("Bảng này không tồn tại");
            }
        }
        #endregion[Function]

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertBranch();
            }
            else
            {
                UpdateBranch();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsBranch = new Branch_BLL();
            var dtBranch = clsBranch.GetBranch();
            if (dtBranch != null && dtBranch.Rows.Count > 0)
            {
                foreach (DataRow dr in dtBranch.Rows)
                {
                    if (txtMaChiNhanh.Text == dr["MACHINHANH"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã chi nhánh !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    else if (txtTenChiNhanh.Text == dr["TENCHINHANH"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng tên chi nhánh !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {

        }
        protected void InsertBranch()
        {
            try
            {
                if (CheckInsert() == false)
                {
                    var obj = new Branch_ET
                    {
                        Branch_Alias = txtMaChiNhanh.Text,
                        Branch_Name = txtTenChiNhanh.Text,
                        Branch_Note = txtGhiChu.Text
                    };
                    Branch_BLL.InsertBranch(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaChiNhanh.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateBranch()
        {
            try
            {
                var obj = new Branch_ET
                {
                    Branch_Id = int.Parse(lbID.Value),
                    Branch_Alias = txtMaChiNhanh.Text,
                    Branch_Name = txtTenChiNhanh.Text,
                    Branch_Note = txtGhiChu.Text
                };
                Branch_BLL.UpdateBranch(obj);
                ((Page.branch)Page).GetBranch();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaChiNhanh.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaChiNhanh.Text = string.Empty;
            txtTenChiNhanh.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
        }
        #endregion
    }
}